package cn.hdu.liang.ch6;

import cn.hdu.liang.ch4.Dish;

import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.Optional;

import static cn.hdu.liang.ch4.Dish.menu;
import static java.util.stream.Collectors.*;


public class Summarizing {
    public static void main(String[] args) {

        System.out.println(howManyDishes());

        Comparator<Dish> dishCaCmp = Comparator.comparingInt(Dish::getCalories);
        Optional<Dish> mostCaDish = menu.stream().collect(maxBy(dishCaCmp));
        System.out.println(mostCaDish.get());

        Integer totalCa = menu.stream().collect(summingInt(Dish::getCalories));
        System.out.println(totalCa);

        Double avg = menu.stream().collect(averagingInt(Dish::getCalories));
        System.out.println(avg.doubleValue());

        IntSummaryStatistics menuStatistics = menu.stream().collect(summarizingInt(Dish::getCalories));
        System.out.println(menuStatistics);

        String shortMenu = menu.stream().map(Dish::getName).collect(joining());
        System.out.println(shortMenu);

        shortMenu = menu.stream().map(Dish::getName).collect(joining(", "));
        System.out.println(shortMenu);

    }


    private static long howManyDishes() {
        return menu.stream().collect(counting());
        //return menu.stream().count();
    }

}
